package com.wss.service.impl;

import com.wss.dao.BlogDao;
import com.wss.domain.Blog;
import com.wss.domain.Message;
import com.wss.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("blogService")
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogDao blogDao;

    @Override
    public List<Blog> findBlogAll() {
        System.out.println("业务层：查询所有博客...");
        return blogDao.findBlogAll();
    }

    @Override
    public List<Blog> findBlogTop8() {
        System.out.println("业务层：查询最新8条博客...");
        return blogDao.findBlogTop8();
    }

    @Override
    public Blog findBlogById(int id) {
        System.out.println("业务层：根据id查询博客...");
        return blogDao.findBlogById(id);
    }

    @Override
    public List<Blog> findBlogByTypeId(int btype_id) {
        System.out.println("业务层：根据btype_id查询出博客...");
        return blogDao.findBlogByTypeId(btype_id);
    }

    @Override
    public List<Blog> findBlogByUid(int u_id) {
        System.out.println("业务层：根据u_id查询出博客...");
        return blogDao.findBlogByUid(u_id);
    }

    @Override
    public List<Blog> findBlogBySearchInput(String input) {
        System.out.println("业务层：根据输入框内容查询出博客...");
        List<Blog> blogList = blogDao.findBlogByInputFromBlogTable(input);
        return blogList;
    }

    @Override
    public Message addBlog(Blog blog) {
        System.out.println("业务层：添加博客...");
        Message message = new Message();
        int res = blogDao.addBlog(blog);
        if (res > 0){
            message.setResCode(1);
            message.setResultMsg("发表成功");
        }else{
            message.setResCode(0);
            message.setResultMsg("发表失败");
        }
        return message;
    }

    @Override
    public Message changeBlog(Blog blog) {
        System.out.println("业务层：修改博客...");
        Message message = new Message();
        int res = blogDao.changeBlog(blog);
        if (res > 0){
            message.setResCode(1);
            message.setResultMsg("修改成功");
        }else{
            message.setResCode(0);
            message.setResultMsg("修改失败");
        }
        return message;
    }

    @Override
    public Message deleteBlog(int id) {
        System.out.println("业务层：删除博客极其评论...");
        Message message = new Message();
        int resBlog = blogDao.deleteCommentsWithBid(id);
        int res = blogDao.deleteBlog(id);
        if (res > 0){
            message.setResCode(1);
            message.setResultMsg("删除成功");
        }else{
            message.setResCode(0);
            message.setResultMsg("删除失败");
        }
        return message;
    }
}
